﻿/**
 * ...
 * @author ...
 * @eamil $(email)
 */
package com.addcn.texas.view.components
{	
	import com.addcn.texas.event.FriendItemEvent;
	import flash.display.MovieClip;
	import flash.display.Sprite;	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	
	public class FriendItiemList extends MovieClip 
	{			
		private var _list : Object;  //好友對象，所以為好要id
		private var _item_arr : Array; 
		private var _counter : int;
		private var _current_click : Object;
		private var _click_type : Boolean; //用于分别鼠标双击 与单击
		
		function FriendItiemList() : void
		{
			this._item_arr = [];
			this._list = { };
			this._counter = 0;
		}
		
		/**
		 * 設置好友列表數據
		 * @param	r_item 
		 */
		public function addItemd(r_item : FriendItem) : void
		{
			var _id = r_item.id;	
			this.addChild(r_item);
			r_item.y = _counter * r_item.height + (_counter + 1) * 3;	
			r_item.out();			
			r_item.name = _id;
			r_item.doubleClickEnabled = true;			
			r_item.addEventListener(MouseEvent.CLICK, _clickHandler);			
			r_item.addEventListener(MouseEvent.ROLL_OUT, _outHandler);			
			r_item.addEventListener(MouseEvent.ROLL_OVER, _overHandler);			
			r_item.addEventListener(MouseEvent.DOUBLE_CLICK, _doubleClickHandler);					
			this._list[_id] = r_item;
			this._item_arr.push(_id);
			this._counter++;			
		}
		
		/**
		 * 移除所有好友列表數據
		 */
		public function removerAllItem() : void
		{
			var _val : FriendItem;
			for (var k in _list)
			{
				_val = _list[k];
				if(_val.parent){
					_val.parent.removeChild(_val);
				}				
			}
			this._item_arr = [];
			this._list = { };
			this._counter = 0;
		}
		
		/**
		 * 設置玩家頭像
		 * @param	r_id int 玩家ID
		 * @param	r_obj Object 頭像對象
		 */
		public function setPhoto(r_id : int, r_obj : Object) : void
		{
			var _obj : FriendItem = this._list[r_id];
			if (_obj) {
				_obj.setPhoto(r_obj);
			}
		}
		
		
		/**
		 * ======================================================
		 * 相應鼠標事件
		 * =====================================================
		 */
		private function _clickHandler(evt : Event) : void
		{ 
			if (this._current_click ) {
				this._current_click.out();
			}
			evt.currentTarget.click();
			this._current_click = evt.currentTarget;	
			this._click_type = false;
			
			var time = new Timer(260, 1);
			time.start()
			time.addEventListener(TimerEvent.TIMER, _checkClick);
		}
		private function _doubleClickHandler(evt : Event) : void
		{	
			this._click_type = true;
						
		}
		private function _checkClick(evt : TimerEvent) : void
		{		
			evt.target.removeEventListener(TimerEvent.TIMER, _checkClick);
			if (this._click_type) { 
				//双击
				_dispatchEvent(FriendItemEvent.ITEM_DOUBLE_CLICK, _current_click.id);
			}else{ 
				//点击
				_dispatchEvent(FriendItemEvent.ITEM_CLICK, _current_click.id);
			}
		}
		private function _outHandler(evt : Event) : void
		{			
			if(evt.currentTarget.state != "click"){
				evt.currentTarget.out();
			}				
			_dispatchEvent(FriendItemEvent.ITEM_OUT, evt.currentTarget.id);
		}
		private function _overHandler(evt : Event) : void
		{
			if(evt.currentTarget.state != "click"){
				evt.currentTarget.over();
			}					
			_dispatchEvent(FriendItemEvent.ITEM_OVER, evt.currentTarget.id);
		}		
		private function _dispatchEvent(r_evt : String, r_id : int) : void
		{
			var _event : FriendItemEvent = new FriendItemEvent(r_evt, {id:r_id});
			dispatchEvent(_event);
		}
	}
	
}